#include "application.h"
#include "AP_setting.h"
#include "State_setting.h"


Overlap_State_Setting_Function State_Seting;
extern Overlap_Setting_Function Seting;
extern INT32U display_left_rec_time;

void overlap_state_setting_function_initial(void)
{
	#if C_TOUCH_PANEL
	State_Seting.USB_select_entry_ptr	= &USB_select_entry_TP;
	State_Seting.state_setting_entry_ptr= &state_setting_entry_TP;
	#else
	State_Seting.USB_select_entry_ptr	= &USB_select_entry;
	State_Seting.state_setting_entry_ptr= &state_setting_entry;
	#endif

}

INT8U USB_select_entry(void *para)
{
#if GP_UI == General_UI
	EXIT_FLAG_ENUM	exit_flag = EXIT_RESUME;
	INT32U			msg_id, prev_state, prev_state1;
	INT8U			curr_tag, sub_tag;
	STAudioConfirm	*audio_temp;

	curr_tag = 0;
	sub_tag = 0xFF;
	prev_state = prev_state1 = *((INT32U*) para);

	ap_USB_setting_page_draw(prev_state, prev_state1, &curr_tag);

	DBG_PRINT("%s ... \r\n", __func__);

	while(exit_flag == EXIT_RESUME)
	{
		if(msgQReceive(ApQ, &msg_id, (void *) ApQ_para, AP_QUEUE_MSG_MAX_LEN) == STATUS_FAIL)
		{
			continue;
		}

		switch(msg_id)
		{
		case EVENT_APQ_ERR_MSG:
			audio_temp = (STAudioConfirm *) ApQ_para;
			if((audio_temp->result == AUDIO_ERR_DEC_FINISH) && (audio_temp->source_type == AUDIO_SRC_TYPE_APP_RS))
			{
				//gpio_write_io(SPEAKER_EN_PIN, DATA_LOW);
			}
			else
			{
				audio_confirm_handler((STAudioConfirm *) ApQ_para);
			}

			break;

		case MSG_STORAGE_SERVICE_MOUNT:
			ap_state_handling_storage_id_set(ApQ_para[0]);
			DBG_PRINT("[Setting Mount OK]\r\n");
			break;

		case MSG_STORAGE_SERVICE_NO_STORAGE:
			ap_state_handling_storage_id_set(ApQ_para[0]);
			DBG_PRINT("[Setting Mount FAIL]\r\n");
			break;

		case MSG_APQ_POWER_KEY_ACTIVE:
		case MSG_APQ_SW_RESET:
			OSTimeDly(1);
			switch(msg_id)
			{
				case MSG_APQ_POWER_KEY_ACTIVE:
					ap_state_handling_power_off(0);
				break;
				case MSG_APQ_SW_RESET:
					ap_state_handling_power_off(1);
				break;
			}
			break;

		case MSG_APQ_DISCONNECT_TO_PC:
			curr_tag = (INT8U)~0;
			msgQSend(ApQ, MSG_APQ_DISCONNECT_TO_PC, NULL, 0, MSG_PRI_NORMAL);
			exit_flag = EXIT_BREAK;
			break;

		case MSG_APQ_FUNCTION_KEY_ACTIVE:
			exit_flag = EXIT_BREAK;
			break;

		case MSG_APQ_NEXT_KEY_ACTIVE:
		case MSG_APQ_PREV_KEY_ACTIVE:
			ap_USB_setting_direction_key_active(&curr_tag, &sub_tag, msg_id, prev_state, prev_state1);
			break;

		case MSG_APQ_BACK_SENSOR_PLUG_IN:
		case MSG_APQ_BACK_SENSOR_PLUG_OUT:
			if(msg_id == MSG_APQ_BACK_SENSOR_PLUG_IN)
			{
				back_sensor_plug_status_set(1);

				//restoring from config and active
				DBG_PRINT("BACK SENSOR PLUG IN\r\n");
			#if C_REAR_ONLY == CUSTOM_OFF
				preview_mode = ap_state_config_display_mode_get();
			#else
				preview_mode = PREVIEW_MODE_REAR;
			#endif
			}
			else
			if(msg_id == MSG_APQ_BACK_SENSOR_PLUG_OUT)
			{
				back_sensor_plug_status_set(0);

				//storing config
				DBG_PRINT("BACK SENSOR PLUG OUT\r\n");

				//de-activate dual cam preview modes
			#if C_REAR_ONLY == CUSTOM_OFF
				ap_state_config_display_mode_set(PREVIEW_MODE_PIP_REAR);
				preview_mode = PREVIEW_MODE_FRONT;
			#else
				ap_state_config_display_mode_set(PREVIEW_MODE_REAR);
				preview_mode = PREVIEW_MODE_NONE;
			#endif
			}

			break;

		default:
			ap_state_common_handling(msg_id);
			break;
		}
	}

	if(msg_id == MSG_APQ_FUNCTION_KEY_ACTIVE)
	{
		if(curr_tag == 0)
		{
			ap_state_config_usb_mode_set(0);
		}
		else
		{
			ap_state_config_usb_mode_set(1);
		}

		ap_USB_setting_display_ok();
	}

	return curr_tag;
#else
	#if 1
	// as usb cam by default
	return 1;
	#else
	// as msdc by default
	return 0;
	#endif
#endif
}

void state_setting_init(INT32U prev_state, INT32U prev_state1, INT8U *tag)
{
	//	INT8U	night_mode;
	DBG_PRINT("%s ... \r\n", __func__);
	DBG_PRINT("setting state init enter\r\n");

	//	night_mode = 0;
	//	msgQSend(PeripheralTaskQ, MSG_PERIPHERAL_TASK_NIGHT_MODE_SET, &night_mode, sizeof(INT8U), MSG_PRI_NORMAL);
	overlap_setting_function_initial();
	Seting.ap_setting_init_ptr(prev_state, prev_state1, tag);
}

void state_setting_exit(INT32U prev_state)
{
	Seting.ap_setting_exit_ptr(prev_state);
	DBG_PRINT("Exit setting state\r\n");
}
OS_Q_DATA q_data1;
extern INT32U browse_last_frame_buf;
void state_setting_entry(void *para)
{
	EXIT_FLAG_ENUM	exit_flag = EXIT_RESUME;
	INT32U			msg_id, prev_state, prev_state1;
	INT8U			curr_tag, sub_tag, temp;
	STAudioConfirm	*audio_temp;
	STOR_SERV_PLAYINFO file_info_ptr;
	struct stat_t buf_tmp;
	INT8U ret = 0;
	curr_tag = 0;
	sub_tag = 0xFF;
	DBG_PRINT("*((INT32U *) para) = 0x%08x \r\n", *((INT32U *) para));
	if ((*((INT32U *) para) & 0xAA000000) == 0xAA000000)
	{
		OSQPost(DisplayTaskQ, (void *) MSG_DISPLAY_TASK_EFFECT_INIT);
		prev_state = STATE_BROWSE;
		prev_state1 = *((INT32U *) para) & 0xAA000000;	// mark it, when exit to return browse state
		DBG_PRINT("coming operate file...\r\n");
	}
	else
	{
		OSQPost(DisplayTaskQ, (void *) MSG_DISPLAY_TASK_EFFECT_INIT);
		prev_state = prev_state1 = (*((INT32U *) para) & 0x1ff);// = STATE_SETTING;
		DBG_PRINT("prev_state = %x\r\n", prev_state);
		DBG_PRINT("coming setting...\r\n");
	}
	//prev_state = prev_state1 = *((INT32U *) para);// = STATE_SETTING;//defualt state is state_setting

	if (ap_go_to_format_get() == IS_FORMAT_IN_VIDEO)
	{
		ap_go_to_format_set(IS_FORMAT_IN_NONE);
		curr_tag = 9;	//selsect the format
	}
 	//msgQSend(ApQ, MSG_APQ_SETTING_TICK, NULL, 0, MSG_PRI_NORMAL);
	//OSQQuery(ApQ->msg_Q_sem, &q_data1);
	//DBG_PRINT("q_data1->OSNMsgs = %d\r\n", q_data1.OSNMsgs);
	//DBG_PRINT("q_data1->OSQSize = %d\r\n", q_data1.OSQSize);
	
	state_setting_init(prev_state, prev_state1, &curr_tag);

	ap_setting_state_tick_set(3, 1);
	
	DBG_PRINT("%s ... \r\n", __func__);

	while(exit_flag == EXIT_RESUME)
	{
		if(msgQReceive(ApQ, &msg_id, (void *) ApQ_para, AP_QUEUE_MSG_MAX_LEN) == STATUS_FAIL)
		{
			continue;
		}
		//DBG_PRINT("nsg_id = 0x%08x\r\n", msg_id);
		switch(msg_id)
		{
		case EVENT_APQ_ERR_MSG:
			audio_temp = (STAudioConfirm *) ApQ_para;
			if((audio_temp->result == AUDIO_ERR_DEC_FINISH) && (audio_temp->source_type == AUDIO_SRC_TYPE_APP_RS))
			{
				//gpio_write_io(SPEAKER_EN_PIN, DATA_LOW);
			}
			else
			{
				audio_confirm_handler((STAudioConfirm *) ApQ_para);
			}

			break;

		case MSG_STORAGE_SERVICE_MOUNT:
			ap_state_handling_icon_show_cmd(ICON_SD_CARD, 0, 0);
			ap_state_handling_icon_clear_cmd(ICON_INTERNAL_MEMORY, 0, 0);
			ap_state_handling_storage_id_set(ApQ_para[0]);
			DBG_PRINT("[Setting Mount OK]\r\n");
			break;

		case MSG_STORAGE_SERVICE_NO_STORAGE:
			ap_state_handling_icon_show_cmd(ICON_INTERNAL_MEMORY, 0, 0);
			ap_state_handling_icon_clear_cmd(ICON_SD_CARD, 0, 0);
			ap_state_handling_storage_id_set(ApQ_para[0]);
			DBG_PRINT("[Setting Mount FAIL]\r\n");
			break;


		case MSG_APQ_POWER_KEY_ACTIVE:
		case MSG_APQ_SW_RESET:
			if(g_setting_reply_action_flag == 0)
			{
				OSTimeDly(2);
				msgQSend(ApQ, msg_id, NULL, 0, MSG_PRI_NORMAL);
				break;
			}

			video_calculate_left_recording_time_disable();
			Seting.ap_setting_exit_ptr(prev_state1);
			OSTimeDly(1);
			switch(msg_id)
			{
				case MSG_APQ_POWER_KEY_ACTIVE:
					ap_state_handling_power_off(0);
				break;
				case MSG_APQ_SW_RESET:
					ap_state_handling_power_off(1);
				break;
			}
			break;
	
		case MSG_APQ_SETTING_TICK:
			//DBG_PRINT("setting tick\r\n");
			if(g_setting_reply_action_flag == 0)
			{
				break;
			}
			ap_setting_tick_draw(&curr_tag, &sub_tag, msg_id, prev_state, prev_state1);
			break;
			
		case MSG_APQ_FUNCTION_KEY_ACTIVE:
			ret = Seting.ap_setting_func_key_active_ptr(&curr_tag, &sub_tag, prev_state, prev_state1);
			if (ret == 0xAA)
			{
				ap_go_to_format_set(IS_FORMAT_IN_SETTING);
				DBG_PRINT("go video\r\n");
				exit_flag = EXIT_BREAK;
				OSQPost(StateHandlingQ, (void *) STATE_VIDEO_RECORD);
			}
			break;

		case MSG_APQ_NEXT_KEY_ACTIVE:
		case MSG_APQ_PREV_KEY_ACTIVE:
			if(g_setting_reply_action_flag == 0)
			{
				//OSTimeDly(1);
				//msgQSend(ApQ, msg_id, NULL, 0, MSG_PRI_NORMAL);
				break;
			}

			Seting.ap_setting_direction_key_active_ptr(&curr_tag, &sub_tag, msg_id, prev_state, prev_state1);
			break;

		case MSG_APQ_MODE:
		case MSG_APQ_RIGHT_KEY_ACTIVE:
			if(g_setting_reply_action_flag == 0)
			{
				//OSTimeDly(1);
				//msgQSend(ApQ, msg_id, NULL, 0, MSG_PRI_NORMAL);
				break;
			}

			exit_flag = Seting.ap_setting_menu_key_active_ptr(&curr_tag, &sub_tag, &prev_state, &prev_state1);
			if(exit_flag == EXIT_BREAK)
			{
				OSTimeDly(1);
			}

			break;


		
		case MSG_APQ_MENU_KEY_ACTIVE:
			if(g_setting_reply_action_flag == 0)
			{
				OSTimeDly(2);
				msgQSend(ApQ, msg_id, NULL, 0, MSG_PRI_NORMAL);
				break;
			}

			exit_flag = Seting.ap_setting_mode_key_active_ptr(prev_state1, &sub_tag);
			break;

		case MSG_STORAGE_SERVICE_BROWSE_REPLY:
			
			gp_memcpy((INT8S *) &file_info_ptr, (INT8S *) ApQ_para, sizeof(STOR_SERV_PLAYINFO));
			DBG_PRINT("file_info_ptr.file_path_addr = %s\r\n", (INT8U*)file_info_ptr.file_path_addr);
			if (file_info_ptr.err_flag == STOR_SERV_OPEN_OK)
			{
				stat((CHAR *) file_info_ptr.file_path_addr, &buf_tmp);			//check this file is Locked or not
				if(buf_tmp.st_mode & D_RDONLY)
				{
					ap_state_handling_icon_show_cmd(ICON_LOCKED, 0, 0);
				}
				gp_memset((void *) browse_last_frame_buf, 0, 320 * 20 * 2);
				ret = ap_state_handling_jpeg_decode(&file_info_ptr, browse_last_frame_buf + 320 * 20 * 2);
				DBG_PRINT("ret = %d\r\n", ret);
			}
			else
			{
				ap_state_handling_icon_clear_cmd(ICON_LOCKED, 0, 0);
				DBG_PRINT("file_info_ptr.err_flag = %d\r\n", file_info_ptr.err_flag);
			}
			//g_setting_reply_action_flag = 1;
			close(((STOR_SERV_PLAYINFO *) ApQ_para)->file_handle);
			//msgQSend(StorageServiceQ, MSG_STORAGE_SERVICE_TIMER_START, NULL,0,MSG_PRI_NORMAL);
			break;

		case MSG_APQ_CONNECT_TO_PC:
		//case MSG_APQ_BATTERY_LOW_SHOW:
			if(g_setting_reply_action_flag == 0)
			{
				OSTimeDly(20);
				msgQSend(ApQ, msg_id, NULL, 0, MSG_PRI_NORMAL);
				break;
			}

			exit_flag = Seting.ap_setting_mode_key_active_ptr(prev_state1, &sub_tag);
			exit_flag = EXIT_BREAK;
			break;

		case MSG_STORAGE_SERVICE_FORMAT_REPLY:
			//video_encode_sensor_off();
			//OSTimeDly(10);
			
			g_setting_reply_action_flag = 1;
			Seting.ap_setting_format_reply_ptr(&curr_tag, prev_state, prev_state1);
			ap_setting_state_tick_set(3, 1);
			DBG_PRINT("setting format done!\r\n");
			msgQSend(StorageServiceQ, MSG_STORAGE_SERVICE_TIMER_START, NULL, 0, MSG_PRI_NORMAL);
			break;

#if C_SCREEN_SAVER == CUSTOM_ON

		case MSG_APQ_KEY_IDLE:
			ap_state_handling_lcd_backlight_switch(0);
			break;

		case MSG_APQ_KEY_WAKE_UP:
			ap_state_handling_lcd_backlight_switch(1);
			break;
#endif

		case MSG_APQ_SELECT_FILE_DEL_REPLY:
			if(g_setting_reply_action_flag == 0)
			{
				//OSTimeDly(20);
				//msgQSend(ApQ, msg_id, NULL, 0, MSG_PRI_NORMAL);
				//break;
			}
			if(ApQ_para[0] == 0x55)
			{
				DBG_PRINT("MSG_APQ_SELECT_FILE_DEL_REPLY is lock...\r\n");
				
				Seting.ap_setting_del_protect_file_show_ptr(&curr_tag, prev_state, prev_state1);
				ap_setting_state_tick_set(3, 1);
				g_setting_reply_action_flag = 1;
				OSQPost(StateHandlingQ, (void *) (STATE_BROWSE | 0xF5F50000));
				exit_flag = EXIT_BREAK;
				break;
			}
			
			Seting.ap_setting_del_reply_ptr(&curr_tag, prev_state, prev_state1);
			ap_setting_state_tick_set(3, 1);
			g_setting_reply_action_flag = 1;
			DBG_PRINT("MSG_APQ_SELECT_FILE_DEL_REPLY...\r\n");
			
			//ap_setting_other_reply();
			OSQPost(StateHandlingQ, (void *) (STATE_BROWSE | 0xA5A50000));

			//wait for display queue empty
			//OSTimeDly(1);
			exit_flag = EXIT_BREAK;
			break;

		case MSG_APQ_FILE_DEL_ALL_REPLY:
		case MSG_APQ_FILE_LOCK_ONE_REPLY:
		case MSG_APQ_FILE_LOCK_ALL_REPLY:
		case MSG_APQ_FILE_UNLOCK_ONE_REPLY:
		case MSG_APQ_FILE_UNLOCK_ALL_REPLY:
			
			if(g_setting_reply_action_flag == 0)
			{
				//OSTimeDly(20);
				//msgQSend(ApQ, msg_id, NULL, 0, MSG_PRI_NORMAL);
				//break;
			}
			//g_setting_reply_action_flag = 1;
			ap_setting_other_reply();
			//OSQPost(StateHandlingQ, (void *) STATE_BROWSE);

			Seting.ap_setting_del_reply_ptr(&curr_tag, prev_state, prev_state1);
			ap_setting_state_tick_set(3, 1);
			g_setting_reply_action_flag = 1;

			//wait for display queue empty
			//OSTimeDly(1);
			if (msg_id == MSG_APQ_FILE_DEL_ALL_REPLY)
				OSQPost(StateHandlingQ, (void *) (STATE_BROWSE | 0xD5D50000));
			else if (msg_id == MSG_APQ_FILE_LOCK_ONE_REPLY)
				OSQPost(StateHandlingQ, (void *) (STATE_BROWSE | 0xC5C50000));
			else if (msg_id == MSG_APQ_FILE_UNLOCK_ONE_REPLY)
				OSQPost(StateHandlingQ, (void *) (STATE_BROWSE | 0x55550000));
			else if (msg_id == MSG_APQ_FILE_UNLOCK_ALL_REPLY)
				OSQPost(StateHandlingQ, (void *) (STATE_BROWSE | 0xE5E50000));
			else
				OSQPost(StateHandlingQ, (void *) (STATE_BROWSE | 0xA5A50000));
			exit_flag = EXIT_BREAK;
			DBG_PRINT("wwwwwwwwww\r\n");
			break;

		case MSG_APQ_USER_CONFIG_STORE:
			ap_state_config_store();
			break;

#if 0//C_BATTERY_DETECT == CUSTOM_ON

			case MSG_APQ_BATTERY_LVL_SHOW:
				ap_state_handling_battery_icon_show(ApQ_para[0]);
				break;

			case MSG_APQ_BATTERY_CHARGED_SHOW:
				ap_state_handling_charge_icon_show(1);
				break;

			case MSG_APQ_BATTERY_CHARGED_CLEAR:
				ap_state_handling_charge_icon_show(0);
				break;

			case MSG_APQ_BATTERY_LOW_SHOW:
				ap_state_handling_clear_all_icon();
				OSTimeDly(1);
				ap_state_handling_str_draw(STR_BATTERY_LOW, WARN_STR_COLOR);
				ap_state_handling_str_draw1(STR_PROMPT, WARN_STR_COLOR);
				msgQSend(PeripheralTaskQ, MSG_PERIPHERAL_TASK_BATTERY_LOW_BLINK_START, NULL, 0, MSG_PRI_NORMAL);
				break;
#endif // C_BATTERY_DETECT == CUSTOM_ON


		case MSG_APQ_BACK_SENSOR_PLUG_IN:
		case MSG_APQ_BACK_SENSOR_PLUG_OUT:
			if(msg_id == MSG_APQ_BACK_SENSOR_PLUG_IN)
			{
				back_sensor_plug_status_set(1);

				//restoring from config and active
				DBG_PRINT("BACK SENSOR PLUG IN\r\n");
			#if C_REAR_ONLY == CUSTOM_OFF
				preview_mode = ap_state_config_display_mode_get();
			#else
				preview_mode = PREVIEW_MODE_REAR;
			#endif
			}
			else
			if(msg_id == MSG_APQ_BACK_SENSOR_PLUG_OUT)
			{
				back_sensor_plug_status_set(0);

				//storing config
				DBG_PRINT("BACK SENSOR PLUG OUT\r\n");

				//de-activate dual cam preview modes
			#if C_REAR_ONLY == CUSTOM_OFF
				ap_state_config_display_mode_set(PREVIEW_MODE_PIP_REAR);
				preview_mode = PREVIEW_MODE_FRONT;
			#else
				ap_state_config_display_mode_set(PREVIEW_MODE_REAR);
				preview_mode = PREVIEW_MODE_NONE;
			#endif
			}

			break;

		case MSG_APQ_BACK_A_CAR_ON:
			if(g_setting_reply_action_flag == 0)
			{
				OSTimeDly(2);
				msgQSend(ApQ, msg_id, NULL, 0, MSG_PRI_NORMAL);
				break;
			}

			DBG_PRINT("setting status BACK A CAR ON\r\n");

			switch(prev_state1)
			{
			case STATE_VIDEO_RECORD:
			case STATE_PHOTO_CAPTURE:
				OSQPost(StateHandlingQ, (void *) prev_state1);
				break;

			default:
				OSQPost(StateHandlingQ, (void *) STATE_VIDEO_RECORD);
				break;
			}

			exit_flag = EXIT_BREAK;
			msgQSend(ApQ, MSG_APQ_BACK_A_CAR_ON, NULL, 0, MSG_PRI_NORMAL);
			temp = BACK_A_CAR_KEY;
			msgQSend(PeripheralTaskQ, MSG_PERIPHERAL_TASK_KEY_REGISTER, &temp, sizeof(INT8U), MSG_PRI_NORMAL);
			break;

		#if defined(C_FIRMWARE_UPGRADE) && C_FIRMWARE_UPGRADE
		case MSG_STORAGE_FIRMWARE_UPGRADE:
			OSQPost(StateHandlingQ, (void *) STATE_FIRMWARE_UPGRADE);
			exit_flag = EXIT_BREAK;
			break;
		#endif // defined(C_FIRMWARE_UPGRADE) && C_FIRMWARE_UPGRADE

		default:
			ap_state_common_handling(msg_id);
			break;
		}
	}

	state_setting_exit(prev_state1);

	if(msg_id == MSG_APQ_MODE)
	{
		//msgQFlush(ApQ);
		//msgQSend(ApQ, MSG_APQ_MODE, NULL, 0, MSG_PRI_NORMAL);
	}
	else
	if(msg_id == MSG_APQ_CONNECT_TO_PC)
	{
		msgQFlush(ApQ);
		msgQSend(ApQ, MSG_APQ_CONNECT_TO_PC, NULL, 0, MSG_PRI_NORMAL);
	}
	else
	if(msg_id == MSG_APQ_BATTERY_LOW_SHOW)
	{
		msgQFlush(ApQ);
		msgQSend(ApQ, MSG_APQ_BATTERY_LOW_SHOW, NULL, sizeof(INT8U), MSG_PRI_NORMAL);
	}
}

